package app.Disthanitian.thaistyle.EmerMez;

import java.util.ArrayList;

import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class AddContactListView extends Activity {
	TextView textStatus;
	ListView listView;
	Button AddContact;
	ArrayList<Boolean> cbState;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addcontactlist);

		// AddContact = (Button) findViewById(R.id.btnAddContact);

		/*
		 * AddContact.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // user BoD suggests using
		 * Intent.ACTION_PICK instead of // .ACTION_GET_CONTENT to avoid the
		 * chooser Intent intent = new Intent(Intent.ACTION_GET_CONTENT); // BoD
		 * con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
		 * intent.setType(ContactsContract
		 * .CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
		 * startActivityForResult(intent, 1); } });
		 */
		ArrayList<String> list = new ArrayList<String>();
		list.add("Data on row 1");
		list.add("Data on row 2");
		list.add("Data on row 3");
		list.add("Data on row 4");
		list.add("Data on row 5");
		list.add("Data on row 6");
		list.add("Data on row 7");

		cbState = new ArrayList<Boolean>();
		for (int i = 0; i < list.size(); i++) {
			cbState.add(false);
		}

		//textStatus = (TextView) findViewById(R.id.textStatus);
		listView = (ListView) findViewById(R.id.About);
		listView.setAdapter(new CustomListViewAdapter(getApplicationContext(),
				android.R.id.text1, list));
	}

	public class CustomListViewAdapter extends ArrayAdapter<String> {
		ArrayList<String> STR;
		LayoutInflater INFLATER;
		Context CONTEXT;

		public CustomListViewAdapter(Context context, int textViewResourceId,
				ArrayList<String> objects) {
			super(context, textViewResourceId, objects);
			INFLATER = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			CONTEXT = context;
			STR = objects;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = INFLATER.inflate(R.layout.listview_row, parent, false);
			final int itemRow = position;
			// TextView textView1 = (TextView) row.findViewById(R.id.textView1);
			// textView1.setTextColor(Color.BLUE);
			// textView1.setText(STR.get(itemRow));

			Button btndelete = (Button) row.findViewById(R.id.delete);
			btndelete.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					STR.remove(itemRow);
					cbState.remove(itemRow);
					listView.setAdapter(new CustomListViewAdapter(CONTEXT,
							android.R.id.text1, STR));
				}
			});

			Button btncheck = (Button) row.findViewById(R.id.check);
			btncheck.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					textStatus.setText("Click on btncheck in Row"
							+ String.valueOf(itemRow));
				}
			});

			CheckBox checkbox1 = (CheckBox) row.findViewById(R.id.checkBox1);
			checkbox1.setChecked(cbState.get(itemRow));
			checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
					cbState.set(itemRow, arg1);
					Toast.makeText(
							CONTEXT,
							"Check box in row" + String.valueOf(itemRow)
									+ " is " + String.valueOf(arg1),
							Toast.LENGTH_SHORT).show();
				}
			});

			return row;
		}
	}
}
